package web.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/CookieLastTimeDemo")
public class CookieLastTimeDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应的消息体的数据格式及编码
        response.setContentType("text/html;charset=utf-8");
        //得到cookie对象的数组
        Cookie[] cookies = request.getCookies();
        System.out.println(cookies.length);
        //做一个标记,false表示cookie的键中不含lastTime
        boolean flag = false;
        //非空判断
        if (cookies.length > 2) {
            //遍历数组
            for (Cookie cookie : cookies) {
                //拿到cookie对象的键
                String name = cookie.getName();
                //判断name中是否有lastTime
                if ("lastTime".equals(name)) {
                    //包含,修改标记
                    flag = true;
                    //拿到上次访问的时间
                    String value = cookie.getValue();
                    //url解码
                    value = URLDecoder.decode(value, "utf-8");
                    response.getWriter().write("<h1>欢迎回来~您上次访问的时间为:" + value + "</h1>");
                    //获取现在访问的时间
                    Date date = new Date();
                    //将时间格式化
                    SimpleDateFormat spf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                    String nowTime = spf.format(date);
                    //URL编码,因为Tomcat8版本以后虽然可以识别汉字,但依然不能识别特殊字符(如空格)
                    nowTime = URLEncoder.encode(nowTime, "utf-8");
                    //重新设置cookie的值
                    cookie.setValue(nowTime);
                    //设置cookie的存活时间为一个月
                    cookie.setMaxAge(60 * 60 * 24 * 30);
                    response.addCookie(cookie);
                    break;
                }
            }
        }

        if (cookies.length == 2 || flag == false) {
            //则是第一次访问
            //设置cookie的键值
            //获取当前时间字符串,重新设置cookie的值,重新发送cookie
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
            String value = sdf.format(date);
            //编码
            value = URLEncoder.encode(value, "utf-8");
            //生成cookie对象
            Cookie cookie = new Cookie("lastTime", value);
            //设置cookie存活时间
            cookie.setMaxAge(60 * 60 * 24 * 30); //一个月
            response.addCookie(cookie);//添加cookie到响应头
            response.getWriter().write("<h1>您好~欢迎您首次访问</h1>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
